package com.ship.dispatch.bean.HomePage;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ship.common.core.mapping.Label;
import com.ship.common.core.utils.DateUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import static com.ship.dispatch.util.NumberFormatUtil.numericFormatting;

/**
 * 当前可用船只，拼接会用到的所有字段
 */
@Data
@TableName("sp_homepage")
public class ConHomePageAll{

    /**
     * 合同id
     */
    private Long conId;

    /**
     * 可用船只的imo
     */
    private String imo;

    /**
     *交船日期-开始
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    private Date deliveryDateBegin;

//    /**
//     *交船日期-结束
//     */
//    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
//    private Date deliveryDateEnd;

    /**
     *还船日期-结束
     */
    @Label(value="还船日期-结束")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm")
    private Date redeliveryDateEnd;

    /**
     * 可用船只的名称
     */
    private String shipName;

    /**
     * 发动机类型
     */
    private String primeMoverDescriptiveNarrative;

    /**
     * 船舶信息（发动机类型+舱容+满载蒸发率+空载蒸发率）
     */
    @TableField(exist = false)
    private String shipInfo;

    /**
     *舱容（单位立方米）
     */
    private BigDecimal cabinCapacity;

    /**
     *船保证的满载蒸发率
     */
    private BigDecimal evaporationRate;

    /**
     *船保证的空载蒸发率
     */
    private BigDecimal emptyEvaporationRate;

    /**
     * ais状态（数值）
     */
    private String aisStatus;

    /**
     * ais状态（文字状态）
     */
    private String aisStatusStr;

    /**
     * ais状态（数值）
     */
    private String mmsi;

//    /**
//     * ais更新时间
//     */
//    private String aisStatusTime;


    /**
     *交船日期-拼接值（租期）
     */
    @TableField(exist = false)
    private String deliveryDate;

    public String getDeliveryDate(){
        Date startDate =  getDeliveryDateBegin();
        Date endDate =  getRedeliveryDateEnd();
        if(startDate == null || endDate == null){
            return null;
        }
        String start = DateUtil.date2Str(startDate,"yyyy年MM月dd日");
        String end = DateUtil.date2Str(endDate,"yyyy年MM月dd日");
        return start + "-" + end;
    }

    public String getShipInfo(){
        String primeMoverDescriptiveNarrative1 = getPrimeMoverDescriptiveNarrative();//发动机类型
        BigDecimal cabinCapacity1 = getCabinCapacity();//舱容
        BigDecimal evaporationRate1 = getEvaporationRate();//满载蒸发率
        BigDecimal emptyEvaporationRate1 = getEmptyEvaporationRate();//空载蒸发率

        String result = "";

        if (primeMoverDescriptiveNarrative1 != null){
            result = result + primeMoverDescriptiveNarrative1 + ",";
        }
        if (cabinCapacity1 != null){
            BigDecimal capacityInThousands = cabinCapacity1.divide(BigDecimal.valueOf(1000), 0, RoundingMode.HALF_UP);
            result = result +capacityInThousands + "K";
        }
        result = result + ",";
        if (evaporationRate1 != null){
            result = result + numericFormatting(evaporationRate1)+"%";
        }
        result = result + ",";
        if (emptyEvaporationRate1 !=null){
            result = result + numericFormatting(emptyEvaporationRate1)+"%";
        }

//        if (result != null && result.startsWith(",")) {
//            result = result.substring(1); // 去掉开头的第一个逗号
//        }

        return result;
    }

    public String getAisStatusStr() {
        String resultStatus = "";
        if (!(getAisStatus() == null)) {
            String aisStatus1 = getAisStatus();
            switch (aisStatus1) {
                case "0":
                    resultStatus = "在航";
                    break;
                case "1":
                    resultStatus = "锚泊";
                    break;
                case "2":
                    resultStatus = "未操纵";
                    break;
                case "3":
                    resultStatus = "有限适航性";
                    break;
                case "4":
                    resultStatus = "受船舶吃水限制";
                    break;
                case "5":
                    resultStatus = "系泊";
                    break;
                case "6":
                    resultStatus = "搁浅";
                    break;
                case "7":
                    resultStatus = "从事捕捞";
                    break;
                case "8":
                    resultStatus = "航行中";
                    break;
            }
        }
        return resultStatus;
    }

}
